<%@page import="com.wh.minihelp.user.util.UserUtil"%>
<%@page import="com.wh.minihelp.user.domain.FamilyAccount"%>
<%@page import="com.wh.minihelp.flower.service.FlowerService"%>
<%@page import="com.wh.minihelp.user.domain.ChildInfo"%>
<%@page import="com.wh.minihelp.flower.domain.FamilyFlower"%>
<%@page import="java.util.List"%>
<%@ page language="java" pageEncoding="UTF-8"%>
<html>
<head>
<%@ include file="/WEB-INF/common/init.jsp"%>
<title>总览页面</title>
</head>
<body>
	<%
		FamilyAccount curFa=UserUtil.getUser(request);
		int size = 0;
		boolean isManager=UserUtil.isFamilyManager(null, curFa, false);
		//信息录入改为弹窗形式
	%>
	<div class="ov_child">
		<a href="<%=path%>/u?m=mgr">修改密码、红花类别、小孩信息请点击这里</a>
		<div>
			<%
				List<ChildInfo> childs = (List<ChildInfo>) request.getAttribute("childs");
				List<String> addFlowers = (List<String>) request.getAttribute("addFlowers");
				List<String> subFlowers = (List<String>) request.getAttribute("subFlowers");
				size = childs.size();
				for (int i = 0; i < size; i++) {
					ChildInfo ff = childs.get(i);
			%>
			<div class="yuanjiao bg<%=i % 2%>" id='<%=ff.getId()%>' data='<%=ff.getName()%>'>
				<div>
					<span style="font-size: 150%; color: #fff"><b><%=ff.getName()%></b></span>&nbsp;&nbsp;共记录了<span id='rc'><%=ff.getRecordCount()%></span>次
				</div>
				<div>
					<span id='leaved' style="color: #fff"><%=(ff.getTotals() - ff.getUsed())%></span>个红花[得<span id='totals'><%=ff.getTotals()%></span>&nbsp;;用<span id='used'><%=ff.getUsed()%></span>]
				</div>
				<%if(isManager){%>
				<div style="position: relative; top: -38px; line-height: 0px; text-align: right">
					<a href="javascript:;" onclick="javascript:addCF(this)" class="weui-cell__ft" style="color: #CCDDFF">+记录红花</a>
				</div>
				<%}%>
				<div style="position: relative; top: -15px; line-height: 0px; text-align: right">
					<a href="<%=path%>/f?m=detail&childId=<%=ff.getId()%>" class="weui-cell__ft" style="color: #CCDDFF">>查看明细</a>
				</div>
			</div>
			<br />
			<%
				}
			%>
		</div>
	</div>
	<form id="form1" method="post" action="<%=path%>/f?m=ACF">
		<div class="js_dialog" id="androidDialog1" style="display: none">
			<div class="weui-mask"></div>
			<div class="weui-dialog weui-skin_android">
				<div class="weui-dialog__hd" style='background-color: lightgray;'>
					<strong class="weui-dialog__title" id='js_dialog_title'>记录小孩红花信息</strong>
				</div>
				<div class="weui-dialog__bd" style="line-height: 2em">
					<div>
						<input type="radio" name="oper" value="0" id="add" checked />
						<label for="add">加红花</label>
						<input type="radio" name="oper" value="1" id="sub" />
						<label for="sub">减红花</label>
					</div>
					<div>
						类型：<select id="flowerName" name="flowerName" style='width: 150px'>
							<%
								for (String fname : addFlowers) {
							%>
							<option><%=fname%></option>
							<%
								}
							%>
						</select>
					</div>
					<div>
						数量：
						<input type="text" name="flowerNumber" id="flowerNumber" style='width: 150px' />
					</div>
					<div>对此次增减添加一些说明吧：</div>
					<div>
						<textarea id="flowerDesc" rows="5" style='width: 98%;'></textarea>
					</div>
					<div style='text-align: right'>
						<a href="javascript:;" class="weui-btn weui-btn_mini weui-btn_primary" onclick="cancelAdd()">取消</a>
						<a href="javascript:;" class="weui-btn weui-btn_mini weui-btn_primary" onclick="formReset()">重填</a>
						<a href="javascript:;" class="weui-btn weui-btn_mini weui-btn_primary" onclick="addChildFlower()">完成</a>
					</div>
				</div>
			</div>
		</div>
	</form>
	<%/*最新动态显示在这里*/	%>
	 <div class="ov_child">
	 <iframe src="<%=path%>/f?m=news" id="f1" frameborder="0" width="100%" height="100%"></iframe>
	 </div>
	<div id="loadingToast" style="opacity: 0; display: none;">
		<div class="weui-mask_transparent"></div>
		<div class="weui-toast">
			<i class="weui-loading weui-icon_toast"></i>
			<p class="weui-toast__content">正在保存数据</p>
		</div>
	</div>
	<script type="text/javascript">
var addFlowers=[<%for (String fname : addFlowers) {%>"<%=fname%>",<%}%>];
var subFlowers=[<%for (String fname : subFlowers) {%>"<%=fname%>",<%}%>];

function resetTypeList(){
	$("#flowerName").empty();
	var str="";
	jQuery.each(  $("input[type=radio]:checked").val()=='0'?addFlowers:subFlowers, function(i, f){
		str+="<option>"+f+"</option>"
	});
	$("#flowerName").append(str);
}
$("input[type=radio]").on('click',resetTypeList);

var $androidDialog1 = $('#androidDialog1');
var curChildId=null;
function addCF(e){
	curChildId=e.parentElement.parentElement.id
	$("#js_dialog_title").text("记录["+e.parentElement.parentElement.attributes.data.value+"]红花信息");
	$androidDialog1.css('display','')
}
function formReset(){
	form1.reset();
	resetTypeList();
}
function cancelAdd(){formReset();$androidDialog1.css('display','none')}
function addChildFlower() {
	var num=$("#flowerNumber").val();
	if (!$.isNumeric(num)) {
		error("#flowerNumber","红花数量这里应该写数字！")
		return
	}
	if (num<=0) {
		error("#flowerNumber","红花增减数量应该大于0！")
		return
	}	
	var flowerDesc=$.trim($("#flowerDesc").val());
	if (flowerDesc.length == 0) {
		error("#flowerDesc","写点描述信息吧，不然以后看不懂的！")
		return
	}
	//
	var url="<%=path%>/f?m=ACF";
	var params={}
	var fields = $("select, :radio").serializeArray();
	jQuery.each( fields, function(i, field){
		params[field.name]=field.value
	});
	params['flowerNumber']=num
	params['flowerDesc']=flowerDesc
	params['childId']=curChildId;
    var $loadingToast = $('#loadingToast');
    if ($loadingToast.css('display') != 'none') return;
    $loadingToast.fadeIn(100);
    $androidDialog1.css('display','none')
	$.post(url,params	   
			  ,function(data){
					if(data.success){
						formReset();
						document.getElementById('f1').contentWindow.location.reload(true);
						 $loadingToast.fadeOut(100);
						 $("#"+curChildId+" #rc").text(data.value.recordCount)
						 $("#"+curChildId+" #leaved").text(data.value.totals-data.value.used)
						 $("#"+curChildId+" #totals").text(data.value.totals)
						 $("#"+curChildId+" #used").text(data.value.used)
					}else{
						alert(data.msg)
					}
			   }, "json");
}
</script>